package cn.clj1;

/**
 * 字符迭代
 * */
public class CljCharIterator {

    public CljLinesFromString myLinesFromString;
    // 当前行
    public String line;
    // 当前字符
    private char c = ' ';
    // 当前字符串的 index
    public int i = -1;
    // 行数
    public int lineNumber = 0;
    //  EOF
    public boolean hasSeenEOF = false;

    public CljCharIterator(final CljLinesFromString myLinesFromString)
    {
        this.myLinesFromString = myLinesFromString;
    }

    // 读取下一个字符
    public boolean next() {
        if (i < 0)
        {
            this.line = this.myLinesFromString.readLine();
            if (this.line == null)
            {
                this.hasSeenEOF = true;
                return false;
            }
            this.lineNumber++;
            i++;
        }

        if (i < this.line.length()) {
            c = this.line.charAt(i);
            i++;
        }
        else {
            i = -1;
            c = '\n';  // 行尾字符
        }
        //this.myLinesFromString.readLine();
        return true;
    }

    // 获取当前字符
    public char current() throws Exception {
        if (this.hasSeenEOF == true) {
            //throw new Exception("unexpected EOF");
            throw new Exception("文件异常结束");
        }
        return this.c;
    }

}
